package com.ifox.frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.lang.StringUtils;

import com.ifox.common.FieldTypes;
import com.ifox.entity.IFoxColumn;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class ColumnMappingFrame extends JDialog {

	private static final long serialVersionUID = 8578852312399756561L;
	private JPanel contentPane;
	private Vector<String> srcColumns;
	private Vector<String> targetColumns;
	private JComboBox cmbSrcColumnName;
	private JComboBox cmbTargetColumnName;
	private JComboBox cmbTargetColumnType;
	private JComboBox cmbSrcColumnType;
	private JRadioButton radDirectCopy;
	private JRadioButton radSpecial;
	private JTextArea txtSpecialValue;
	private IFoxColumn iFoxCol; 
	
	public ColumnMappingFrame(Dialog owner, boolean modal, Vector<String> srcColumns,Vector<String> targetColumns) {
		super(owner, null, modal);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				if(iFoxCol!=null){
					cmbSrcColumnName.setSelectedItem(iFoxCol.getSrcColumn());
					cmbSrcColumnType.setSelectedItem(FieldTypes.valueOf(iFoxCol.getSrcType()));
					cmbTargetColumnName.setSelectedItem(iFoxCol.getTargetColumn());
					cmbTargetColumnType.setSelectedItem(FieldTypes.valueOf(iFoxCol.getTargetType()));
					txtSpecialValue.setText(iFoxCol.getSpecialValue());
					if("是".equals(iFoxCol.getDirectCopy())){
						radDirectCopy.setSelected(true);
					}else{
						radSpecial.setSelected(true);
					}
				}
			}
		});
		this.srcColumns = srcColumns;
		this.targetColumns = targetColumns;
		init();
	}
	
	
	private void init(){
		setTitle("列映射规则");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 501, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		contentPane.add(toolBar, BorderLayout.NORTH);
		
		JButton btnSubmit = new JButton("提交");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				IFoxColumn column = new IFoxColumn();
				column.setSrcColumn(cmbSrcColumnName.getSelectedItem().toString());
				column.setSrcType(cmbSrcColumnType.getSelectedItem().toString());
				column.setTargetColumn(cmbTargetColumnName.getSelectedItem().toString());
				column.setTargetType(cmbTargetColumnType.getSelectedItem().toString());
				if(radDirectCopy.isSelected()){
					column.setDirectCopy("是");
					column.setSpecialValue("");
					iFoxCol = column;
					ColumnMappingFrame.this.dispose();
				}else{
					column.setDirectCopy("否");
					String v = txtSpecialValue.getText();
					if(StringUtils.isBlank(v)){
						JOptionPane.showMessageDialog(null, "特殊值不能为空", "信息", JOptionPane.WARNING_MESSAGE);
					}else{
						column.setSpecialValue(v);
						iFoxCol = column;
						ColumnMappingFrame.this.dispose();
					}
				}
			}
		});
		toolBar.add(btnSubmit);
		
		JButton btnQuit = new JButton("关闭");
		btnQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ColumnMappingFrame.this.dispose();
			}
		});
		toolBar.add(btnQuit);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "\u5217\u6620\u5C04", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel label_1 = new JLabel("目标表列名：");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setBounds(264, 34, 72, 15);
		panel.add(label_1);
		
		cmbTargetColumnName = new JComboBox(targetColumns);
		cmbTargetColumnName.setBounds(342, 31, 133, 21);
		panel.add(cmbTargetColumnName);
		
		JLabel label_3 = new JLabel("目标表列类型：");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(252, 71, 84, 15);
		panel.add(label_3);
		
		cmbTargetColumnType = new JComboBox();
		cmbTargetColumnType.setModel(new DefaultComboBoxModel(FieldTypes.values()));
		cmbTargetColumnType.setBounds(342, 68, 133, 21);
		panel.add(cmbTargetColumnType);
		
		ButtonGroup buttonGroup = new ButtonGroup();

		radDirectCopy = new JRadioButton("直接复制");
		radDirectCopy.setSelected(true);
		radDirectCopy.setBounds(6, 109, 78, 23);
		panel.add(radDirectCopy);
		
		radSpecial = new JRadioButton("特殊值");
		radSpecial.setBounds(6, 143, 64, 23);
		panel.add(radSpecial);
		
		buttonGroup.add(radDirectCopy);
		buttonGroup.add(radSpecial);
		
		txtSpecialValue = new JTextArea();
		txtSpecialValue.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				radSpecial.setSelected(true);
			}
		});
		txtSpecialValue.setBounds(75, 143, 349, 63);
		panel.add(txtSpecialValue);
		
		JLabel label = new JLabel("发出表列类型：");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(6, 71, 84, 15);
		panel.add(label);
		
		cmbSrcColumnType = new JComboBox();
		cmbSrcColumnType.setModel(new DefaultComboBoxModel(FieldTypes.values()));
		cmbSrcColumnType.setBounds(96, 68, 133, 21);
		panel.add(cmbSrcColumnType);
		
		cmbSrcColumnName = new JComboBox(srcColumns);
		cmbSrcColumnName.setBounds(96, 31, 133, 21);
		panel.add(cmbSrcColumnName);
		
		JLabel label_2 = new JLabel("发出表列名：");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(18, 34, 72, 15);
		panel.add(label_2);
	}


	public IFoxColumn getiFoxCol() {
		return iFoxCol;
	}


	public void setiFoxCol(IFoxColumn iFoxCol) {
		this.iFoxCol = iFoxCol;
	}
}
